package com.yand.thinkingInJava.thinkingInJava21.ShareResource;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * pair 类
 */
class Pair{

    private int x,y;

    public Pair(int x , int y){
        this.x = x;
        this.y = y;
    }

    public Pair(){this(0,0);}

    public int getX(){return  x;}
    public int getY(){return y;}
    public void incrementX(){ x++;}
    public void incrementY(){ y++;}
    public String toString(){
        return "x: "+x+",y: "+y;
    }

    public class PairValuesNotEqualExcepotion extends RuntimeException{
        public PairValuesNotEqualExcepotion(){
            super("Pair values not equal: "+Pair.this);
        }
    }


    /**
     * 检查状态
     */
    public void checkState(){
        if(x != y){
            throw new PairValuesNotEqualExcepotion();
        }


    }
}


abstract class PairManager{
    AtomicInteger checkCounter = new AtomicInteger(0);

    protected Pair p = new Pair();

    private List<Pair> storage =
            Collections.synchronizedList(new ArrayList<Pair>());

    public synchronized Pair getPair(){
        return new Pair(p.getX(),p.getY());
    }

    protected void store(Pair p){
        storage.add(p);
        try{
            TimeUnit.MILLISECONDS.sleep(50);
        }catch(InterruptedException e){

        }
    }

    public abstract void increment();
}



class PairManager1 extends PairManager{

    @Override
    public synchronized void increment() {
        //x 自增
         p.incrementX();
        //y 自增
         p.incrementY();

         store(getPair());
    }
}

class PairManager2 extends PairManager{

    @Override
    public void increment() {
        Pair temp;
        synchronized (this){
            p.incrementX();
            p.incrementY();
            temp = getPair();
        }
        store(temp);
    }
}

class PairManipulator implements Runnable{

    private PairManager pm;

    public PairManipulator(PairManager pm){
        this.pm = pm;
    }

    @Override
    public void run() {
        while(true){
            pm.increment();
        }
    }

    public String toString(){
        return "Pair: "+pm.getPair()+" checkCounter = "+pm.checkCounter.get();
    }
}



class PairChecker implements Runnable{
    private PairManager pm;
    public PairChecker(PairManager pm){
        this.pm = pm;
    }


    @Override
    public void run() {
        while(true){
            pm.checkCounter.incrementAndGet();
            pm.getPair().checkState();
        }
    }
}

/**
 * 同步代码块
 */
public class CriticalSection {

    static void testApproaches(PairManager pmam1, PairManager pmam2){

        ExecutorService exec = Executors.newCachedThreadPool();
        PairManipulator pm1 = new PairManipulator(pmam1);
        PairChecker pcheck1 = new PairChecker(pmam1);
        PairManipulator pm2 = new PairManipulator(pmam2);
        PairChecker pcheck2 = new PairChecker(pmam2);

        exec.execute(pm1);
        exec.execute(pcheck1);
        exec.execute(pm2);

        exec.execute(pcheck2);

        try{
            TimeUnit.MILLISECONDS.sleep(500);
        }catch (InterruptedException e){
            System.out.println("Sleep interrupted");
        }
        System.out.println("pm1: "+pm1+"\npm2: "+pm2);
        System.exit(0);
    }


    public static void main(String [] args){
        PairManager pman1 = new PairManager1();
        PairManager pman2 = new PairManager2();

        testApproaches(pman1, pman2);
    }



}
